package actions;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import mail.Mail;
import mailDAO.MailDAO;
import DBMailDAO.DBMailDAO;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	

	private String mailId;
	private String fileName;
	private String fileContentType;
	private InputStream fileContent;

	public void setId(String id) {
		this.mailId = id;
	}

	public String getId() {
		return mailId;
	}
	
	public String getMailId() {
		return mailId;
	}

	public void setMailId(String mailId) {
		this.mailId = mailId;
	}

	public String getFileName() {
		try {
			return new String(fileName.getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public InputStream getFileContent() {
		return fileContent;
	}

	public void setFileContent(InputStream fileContent) {
		this.fileContent = fileContent;
	}	
	
	public String download(){
		try{
		MailDAO mailDao = new DBMailDAO();
		Mail mail = mailDao.loadMailById(Long.valueOf(mailId));
		this.fileName = mail.getAttachmentName();
		this.fileContentType = mail.getAttachmentType();
		this.fileContent = mail.getAttachmentContent().getBinaryStream();
		return SUCCESS;
		}catch (Exception e){
			return ERROR;
		}
	}
}
